var app = getApp();
var date = new Date();
var currentHours = date.getHours();
var currentMinute = date.getMinutes();
Page({

  data: {
    status:'',
    statusindex:"",
    choose:'',
    info:{},
    startDate:'请选择开始时间',
    endDate:'请选择结束时间',
    multiArray: [['今天', '明天', '3-2', '3-3', '3-4', '3-5'], [0, 1, 2, 3, 4, 5, 6], [0, 10, 20]],
    multiIndex: [0, 0, 0],
    multiArray2: [['今天', '明天', '3-2', '3-3', '3-4', '3-5'], [0, 1, 2, 3, 4, 5, 6], [0, 10, 20]],
    multiIndex2: [0, 0, 0],
    pageInfo: '',
    index: 0,
    eindex: 0,
    index2: 0,
    array: [],
    earray: [],
    array2: ['上班', '下班'],
    date: '',
    liyou:'',
    workz:'',
    date2:'',
    hou:''
  },

  onLoad: function () {

    var that = this;
    if (!wx.getStorageSync('openid')) {
      app.error("请登录后再操作");
      return false;
    }

    that.getwork();
  },
  formSubmit: function (event) {
    var that = this;
    var sdate = that.data.startDate;
    var edate = that.data.endDate;
 

    if(sdate == "请选择开始时间"){
      app.error('请选择开始时间');
      return false;
    }

    if(edate == "请选择结束时间"){
      app.error('请选择结束时间');
      return false;
    }
 
    var liyou = event.detail.value.liyou;
      if (!liyou) {
        app.error('请输入变更理由');
        return;
      } 
 
    wx.request({
      url: app.apiUrl + '/api/index/huanbanshenqing',
      data: {
        kaishi:sdate,
        jieshu:edate,
        liyou:liyou,
        openid:wx.getStorageSync('openid'),
        qian:this.data.workz,
        hou:this.data.statusindex,
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res=> {
        if(res.data.code == 0){
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            showCancel: false
          });
    
        }else{
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            showCancel: false
          });
          setTimeout(function () {
            wx.navigateTo({
              url: '/page/my/index',
            });
          }, 2000)
       
        }
   
      }
    })



 
 
  
  },


  //选择开始时间 start
  pickerTap:function() {
    date = new Date();
    console.log(date)

    var monthDay = ['今天','明天'];
    var hours = [];
    var minute = [];
    
     currentHours = date.getHours();
     currentMinute = date.getMinutes();

    // 月-日 
    for (var i = 2; i <= 28; i++) {
      var date1 = new Date(date);
      date1.setDate(date.getDate() + i);
      var md = (date1.getMonth() + 1) + "-" + date1.getDate();
      monthDay.push(md);
    }

    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };

    if(data.multiIndex[0] === 0) {
      if(data.multiIndex[1] === 0) {
        this.loadData(hours, minute);
      } else {
        this.loadMinute(hours, minute);
      }
    } else {
      this.loadHoursMinute(hours, minute);
    }

    data.multiArray[0] = monthDay;
    data.multiArray[1] = hours;
    data.multiArray[2] = minute;

    this.setData(data);
  },

bindMultiPickerColumnChange:function(e) {
   date = new Date();

  var that = this;

  var monthDay = ['今天', '明天'];
  var hours = [];
  var minute = [];

   currentHours = date.getHours();
   currentMinute = date.getMinutes();

  var data = {
    multiArray: this.data.multiArray,
    multiIndex: this.data.multiIndex
  };
  // 把选择的对应值赋值给 multiIndex
  data.multiIndex[e.detail.column] = e.detail.value;

  // 然后再判断当前改变的是哪一列,如果是第1列改变
  if (e.detail.column === 0) {
    // 如果第一列滚动到第一行
    if (e.detail.value === 0) {

      that.loadData(hours, minute);
      
    } else {
      that.loadHoursMinute(hours, minute);
    }

    data.multiIndex[1] = 0;
    data.multiIndex[2] = 0;

    // 如果是第2列改变
  } else if (e.detail.column === 1) {

    // 如果第一列为今天
    if (data.multiIndex[0] === 0) {
      if (e.detail.value === 0) {
        that.loadData(hours, minute);
      } else {
        that.loadMinute(hours, minute);
      }
      // 第一列不为今天
    } else {
      that.loadHoursMinute(hours, minute);
    }
    data.multiIndex[2] = 0;

    // 如果是第3列改变
  } else {
    // 如果第一列为'今天'
    if (data.multiIndex[0] === 0) {

      // 如果第一列为 '今天'并且第二列为当前时间
      if(data.multiIndex[1] === 0) {
        that.loadData(hours, minute);
      } else {
        that.loadMinute(hours, minute);
      }
    } else {
      that.loadHoursMinute(hours, minute);
    }
  }
  data.multiArray[1] = hours;
  data.multiArray[2] = minute;
  this.setData(data);
},

loadData: function (hours, minute) {

  var minuteIndex;
  if (currentMinute > 0 && currentMinute <= 10) {
    minuteIndex = 10;
  } else if (currentMinute > 10 && currentMinute <= 20) {
    minuteIndex = 20;
  } else if (currentMinute > 20 && currentMinute <= 30) {
    minuteIndex = 30;
  } else if (currentMinute > 30 && currentMinute <= 40) {
    minuteIndex = 40;
  } else if (currentMinute > 40 && currentMinute <= 50) {
    minuteIndex = 50;
  } else {
    minuteIndex = 60;
  }

  if (minuteIndex == 60) {
    // 时
    for (var i = currentHours + 1; i < 24; i++) {
      hours.push(i);
    }
    // 分
    for (var i = 0; i < 60; i += 10) {
      minute.push(i);
    }
  } else {
    // 时
    for (var i = currentHours; i < 24; i++) {
      hours.push(i);
    }
    // 分
    for (var i = minuteIndex; i < 60; i += 10) {
      minute.push(i);
    }
  }
},

loadHoursMinute: function (hours, minute){
  // 时
  for (var i = 0; i < 24; i++) {
    hours.push(i);
  }
  // 分
  for (var i = 0; i < 60; i += 10) {
    minute.push(i);
  }
},

loadMinute: function (hours, minute) {
  var minuteIndex;
  if (currentMinute > 0 && currentMinute <= 10) {
    minuteIndex = 10;
  } else if (currentMinute > 10 && currentMinute <= 20) {
    minuteIndex = 20;
  } else if (currentMinute > 20 && currentMinute <= 30) {
    minuteIndex = 30;
  } else if (currentMinute > 30 && currentMinute <= 40) {
    minuteIndex = 40;
  } else if (currentMinute > 40 && currentMinute <= 50) {
    minuteIndex = 50;
  } else {
    minuteIndex = 60;
  }

  if (minuteIndex == 60) {
    // 时
    for (var i = currentHours + 1; i < 24; i++) {
      hours.push(i);
    }
  } else {
    // 时
    for (var i = currentHours; i < 24; i++) {
      hours.push(i);
    }
  }
  // 分
  for (var i = 0; i < 60; i += 10) {
    minute.push(i);
  }
},

bindStartMultiPickerChange: function (e) {
  var that = this;
  var monthDay = that.data.multiArray[0][e.detail.value[0]];
  var hours = that.data.multiArray[1][e.detail.value[1]];
  var minute = that.data.multiArray[2][e.detail.value[2]];

  if (monthDay === "今天") {
    var month = date.getMonth()+1;
    var day = date.getDate();
    monthDay = month + "月" + day + "日";
  } else if (monthDay === "明天") {
    var date1 = new Date(date);
    date1.setDate(date.getDate() + 1);
    monthDay = (date1.getMonth() + 1) + "月" + date1.getDate() + "日";

  } else {
    var month = monthDay.split("-")[0]; // 返回月
    var day = monthDay.split("-")[1]; // 返回日
    monthDay = month + "月" + day + "日";
  }

  var startDate = monthDay + " " + hours + ":" + minute;
  that.setData({
    startDate: startDate
  })
},
//选择开始时间 end


  //选择结束时间 start
  pickerTap2:function() {
    date = new Date();
    var monthDay = ['今天','明天'];
    var hours = [];
    var minute = [];
    
     currentHours = date.getHours();
     currentMinute = date.getMinutes();

    // 月-日
    for (var i = 2; i <= 28; i++) {
      var date1 = new Date(date);
      date1.setDate(date.getDate() + i);
      var md = (date1.getMonth() + 1) + "-" + date1.getDate();
      monthDay.push(md);
    }

    var data = {
      multiArray2: this.data.multiArray2,
      multiIndex2: this.data.multiIndex2
    };

    if(data.multiIndex2[0] === 0) {
      if(data.multiIndex2[1] === 0) {
        this.loadData2(hours, minute);
      } else {
        this.loadMinute2(hours, minute);
      }
    } else {
      this.loadHoursMinute2(hours, minute);
    }

    data.multiArray2[0] = monthDay;
    data.multiArray2[1] = hours;
    data.multiArray2[2] = minute;

    this.setData(data);
  },

bindMultiPickerColumnChange2:function(e) {
   date = new Date();

  var that = this;

  var monthDay = ['今天', '明天'];
  var hours = [];
  var minute = [];

   currentHours = date.getHours();
   currentMinute = date.getMinutes();

  var data = {
    multiArray2: this.data.multiArray2,
    multiIndex2: this.data.multiIndex2
  };
  // 把选择的对应值赋值给 multiIndex
  data.multiIndex2[e.detail.column] = e.detail.value;

  // 然后再判断当前改变的是哪一列,如果是第1列改变
  if (e.detail.column === 0) {
    // 如果第一列滚动到第一行
    if (e.detail.value === 0) {

      that.loadData2(hours, minute);
      
    } else {
      that.loadHoursMinute2(hours, minute);
    }

    data.multiIndex2[1] = 0;
    data.multiIndex2[2] = 0;

    // 如果是第2列改变
  } else if (e.detail.column === 1) {

    // 如果第一列为今天
    if (data.multiIndex2[0] === 0) {
      if (e.detail.value === 0) {
        that.loadData2(hours, minute);
      } else {
        that.loadMinute2(hours, minute);
      }
      // 第一列不为今天
    } else {
      that.loadHoursMinute2(hours, minute);
    }
    data.multiIndex2[2] = 0;

    // 如果是第3列改变
  } else {
    // 如果第一列为'今天'
    if (data.multiIndex2[0] === 0) {

      // 如果第一列为 '今天'并且第二列为当前时间
      if(data.multiIndex2[1] === 0) {
        that.loadData2(hours, minute);
      } else {
        that.loadMinute2(hours, minute);
      }
    } else {
      that.loadHoursMinute2(hours, minute);
    }
  }
  data.multiArray2[1] = hours;
  data.multiArray2[2] = minute;
  this.setData(data);
},

loadData2: function (hours, minute) {

  var minuteIndex;
  if (currentMinute > 0 && currentMinute <= 10) {
    minuteIndex = 10;
  } else if (currentMinute > 10 && currentMinute <= 20) {
    minuteIndex = 20;
  } else if (currentMinute > 20 && currentMinute <= 30) {
    minuteIndex = 30;
  } else if (currentMinute > 30 && currentMinute <= 40) {
    minuteIndex = 40;
  } else if (currentMinute > 40 && currentMinute <= 50) {
    minuteIndex = 50;
  } else {
    minuteIndex = 60;
  }

  if (minuteIndex == 60) {
    // 时
    for (var i = currentHours + 1; i < 24; i++) {
      hours.push(i);
    }
    // 分
    for (var i = 0; i < 60; i += 10) {
      minute.push(i);
    }
  } else {
    // 时
    for (var i = currentHours; i < 24; i++) {
      hours.push(i);
    }
    // 分
    for (var i = minuteIndex; i < 60; i += 10) {
      minute.push(i);
    }
  }
},

loadHoursMinute2: function (hours, minute){
  // 时
  for (var i = 0; i < 24; i++) {
    hours.push(i);
  }
  // 分
  for (var i = 0; i < 60; i += 10) {
    minute.push(i);
  }
},

loadMinute2: function (hours, minute) {
  var minuteIndex;
  if (currentMinute > 0 && currentMinute <= 10) {
    minuteIndex = 10;
  } else if (currentMinute > 10 && currentMinute <= 20) {
    minuteIndex = 20;
  } else if (currentMinute > 20 && currentMinute <= 30) {
    minuteIndex = 30;
  } else if (currentMinute > 30 && currentMinute <= 40) {
    minuteIndex = 40;
  } else if (currentMinute > 40 && currentMinute <= 50) {
    minuteIndex = 50;
  } else {
    minuteIndex = 60;
  }

  if (minuteIndex == 60) {
    // 时
    for (var i = currentHours + 1; i < 24; i++) {
      hours.push(i);
    }
  } else {
    // 时
    for (var i = currentHours; i < 24; i++) {
      hours.push(i);
    }
  }
  // 分
  for (var i = 0; i < 60; i += 10) {
    minute.push(i);
  }
},

bindStartMultiPickerChange2: function (e) {
  var that = this;
  var monthDay = that.data.multiArray2[0][e.detail.value[0]];
  var hours = that.data.multiArray2[1][e.detail.value[1]];
  var minute = that.data.multiArray2[2][e.detail.value[2]];

  if (monthDay === "今天") {
    var month = date.getMonth()+1;
    var day = date.getDate();
    monthDay = month + "月" + day + "日";
  } else if (monthDay === "明天") {
    var date1 = new Date(date);
    date1.setDate(date.getDate() + 1);
    monthDay = (date1.getMonth() + 1) + "月" + date1.getDate() + "日";

  } else {
    var month = monthDay.split("-")[0]; // 返回月
    var day = monthDay.split("-")[1]; // 返回日
    monthDay = month + "月" + day + "日";
  }

  var endDate = monthDay + " " + hours + ":" + minute;
  that.setData({
    endDate: endDate
  })
},
//选择结束时间 end

//改变事件结束
  changezt(){
    var that = this
    console.log(that.data.itemList);
    wx.showActionSheet({

      //目前只有这三种  按天-》计件 按时-》计件  计件-》按时
     

      success(res) {
        that.setData({ status: that.data.itemList[res.tapIndex]});
        that.setData({ choose: res.tapIndex});
        console.log(res.tapIndex)
     
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },


 
  getwork(){
    var that = this
  wx.request({
    url: app.apiUrl + '/api/data/getworkchange',
    data: {
      openid:wx.getStorageSync('openid'),
      groupid: wx.getStorageSync('groupid'),
    },
    method: 'POST',
    header: { 'content-type': 'application/x-www-form-urlencoded' },
    success: res=> {
        if(res.data){
          var status = "";
          var statusindex = ""
          console.log(res.data.data)
          that.setData({ info: res.data.data});
          that.setData({ workz: res.data.data.is_hours});
          //目前只有这三种  按天-》计件 按时-》计件  计件-》按时
          if(res.data.data.is_hours == 1){
            status = "计件";
            statusindex = 2;
          }else if(res.data.data.is_hours == 2){
            status = "计时";
            statusindex = 1;
          }else{
            status = "计件";
            statusindex = 2;
          }
       
          that.setData({ status: status});
          that.setData({ statusindex: statusindex});

        }
    }
  })
},

  bindKeyInput(e){
      console.log(e.detail.value)
      this.setData({ liyou: e.detail.value});
  },

  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },

  ebindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      edate: e.detail.value
    })
  },


  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },

  ebindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      eindex: e.detail.value
    })
  },

})